#!/usr/bin/perl


use HTTP::Request::Common qw(POST);
use LWP::UserAgent;



sub getdata {

my $geoURL = "http://api.hostip.info/get_html.php";

print "$_[0]\n";

my $ua=LWP::UserAgent->new();
my $req= POST $geoURL, [ "ip" => $_[0], "position" => "true" ];
$req->content_type('application/x-www-form-urlencoded');

#print "$_->[0] Agent: $_->[1]\n";

# Send Request
my $content=$ua->request($req)->as_string;

# Currenlty Rquired for error checking!
my $check=$ua->request($req);

if ($check->is_success) {
#print $content;

	my @co1 = split /Country: /, $content;
	my @co2 = split /\n/, @co1[1];
	my @ci1 = split /City: /, $content;
	my @ci2 = split /\n/, @ci1[1];
	my $country = @co2[0];
	my $city = @ci2[0];

# images section for country
	my @cc1 = split /\(/, $country;
	my @cc2 = split /\)/, @cc1[1];
	my $country_code= lc(@cc2[0]);
	if($country_code =~ m/unknown country?/i || $country_code =~ m/private address/i){
		$country_code = "none";
	}
#	print "$country_code\n";
	
		
	my @first = split /Latitude: /, $content;
#	print @first[1];

	my @second = split /Longitude: /, @first[1];

	my @third = split /\n/, @second[0];
	my $lat = @third[0];
	my @forth = split /\n/, @second[1];
	my $lon = @forth[0];

	@data = ($lat, $lon, $country, $country_code, $city);
	print "$country\n";
	return(@data)
}
# need to return from function $lat $lon $country $country_code $city
}

my $ip="217.167.123.31";

getdata($ip);
